package drops.gui;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;


public class DateSelectorPanel extends JPanel implements ItemListener {
	
	JComboBox monthDropDown;
	JComboBox dayDropDown;
	JComboBox yearDropDown;
	ComboBoxModel monthModel;
	ComboBoxModel yearModel;
	ComboBoxModel dayModel;
	String [] day31,day29,day28,day30;
	Calendar calendar;
	
	public DateSelectorPanel() {
		String [] monthOptions = {"Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		calendar = Calendar.getInstance();
		monthModel= new DefaultComboBoxModel(monthOptions);
		day28 = new String[28];
		for (int i=0;i<28;i++) {
			day28[i]=i+1 + "";
		}
		day29 = new String[29];
		for (int i=0;i<29;i++) {
			day29[i]=i+1 + "";
		}
		day30 = new String[30];
		for (int i=0;i<30;i++) {
			day30[i]=i+1 + "";
		}
		day31 = new String[31];
		for (int i=0;i<31;i++) {
			day31[i]=i+1 + "";
		}
		
		monthDropDown = new JComboBox(monthModel);
		monthDropDown.addItemListener(this);		
		dayModel = new DefaultComboBoxModel(day31);
		dayDropDown = new JComboBox(dayModel);
		
		String[] yearOptions = new String[50];
		for (int i=0;i<50;i++) {
			yearOptions[i] = i+1980+"";
		}
		
		yearModel = new DefaultComboBoxModel(yearOptions);
		yearDropDown = new JComboBox(yearModel);
		add(monthDropDown);
		add(dayDropDown);
		add(yearDropDown);
		setDate(calendar.getTime());
	}
	
	public DateSelectorPanel(Date date) {
		String [] monthOptions = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		calendar = Calendar.getInstance();
		monthModel= new DefaultComboBoxModel(monthOptions);
		day28 = new String[28];
		for (int i=0;i<28;i++) {
			day28[i]=i+1 + "";
		}
		day29 = new String[29];
		for (int i=0;i<29;i++) {
			day29[i]=i+1 + "";
		}
		day30 = new String[30];
		for (int i=0;i<30;i++) {
			day30[i]=i+1 + "";
		}
		day31 = new String[31];
		for (int i=0;i<31;i++) {
			day31[i]=i+1 + "";
		}
		
		monthDropDown = new JComboBox(monthModel);
		monthDropDown.addItemListener(this);
		dayModel = new DefaultComboBoxModel(day31);
		dayDropDown = new JComboBox(dayModel);
		
		String[] yearOptions = new String[50];
		for (int i=0;i<50;i++) {
			yearOptions[i] = i+1980+"";
		}
		
		yearModel = new DefaultComboBoxModel(yearOptions);
		yearDropDown = new JComboBox(yearModel);
		add(monthDropDown);
		add(dayDropDown);
		add(yearDropDown);
		setDate(date);
	}
	
	public void setDate(Date date) {
		calendar.setTime(date);
		switch (calendar.get(Calendar.MONTH)) {
		case Calendar.JANUARY : monthModel.setSelectedItem("Jan"); break;
		case Calendar.FEBRUARY : monthModel.setSelectedItem("Feb"); break;
		case Calendar.MARCH : monthModel.setSelectedItem("Mar"); break;
		case Calendar.APRIL : monthModel.setSelectedItem("Apr"); break;
		case Calendar.MAY : monthModel.setSelectedItem("May"); break;
		case Calendar.JUNE : monthModel.setSelectedItem("Jun"); break;
		case Calendar.JULY : monthModel.setSelectedItem("Jul"); break;
		case Calendar.AUGUST : monthModel.setSelectedItem("Aug"); break;
		case Calendar.SEPTEMBER : monthModel.setSelectedItem("Sep"); break;
		case Calendar.OCTOBER : monthModel.setSelectedItem("Oct"); break;
		case Calendar.NOVEMBER : monthModel.setSelectedItem("Nov"); break;
		case Calendar.DECEMBER : monthModel.setSelectedItem("Dec"); break;
		}
		updateDaySelection();
		dayModel.setSelectedItem(calendar.get(Calendar.DAY_OF_MONTH)+"");

		yearModel.setSelectedItem(calendar.get(Calendar.YEAR)+"");
		
	}
	
	public Date getDate() {
		String mon = monthModel.getSelectedItem().toString();
		if (mon.compareTo("Jan")==0)
			calendar.set(Calendar.MONTH, Calendar.JANUARY);
		else if (mon.compareTo("Feb")==0)
			calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
		else if (mon.compareTo("Mar")==0)
			calendar.set(Calendar.MONTH, Calendar.MARCH);
		else if (mon.compareTo("Apr")==0)
			calendar.set(Calendar.MONTH, Calendar.APRIL);
		else if (mon.compareTo("May")==0)
			calendar.set(Calendar.MONTH, Calendar.MAY);
		else if (mon.compareTo("Jun")==0)
			calendar.set(Calendar.MONTH, Calendar.JUNE);
		else if (mon.compareTo("Jul")==0)
			calendar.set(Calendar.MONTH, Calendar.JULY);
		else if (mon.compareTo("Aug")==0)
			calendar.set(Calendar.MONTH, Calendar.AUGUST);
		else if (mon.compareTo("Sep")==0)
			calendar.set(Calendar.MONTH, Calendar.SEPTEMBER);
		else if (mon.compareTo("Oct")==0)
			calendar.set(Calendar.MONTH, Calendar.OCTOBER);
		else if (mon.compareTo("Nov")==0)
			calendar.set(Calendar.MONTH, Calendar.NOVEMBER);
		else if (mon.compareTo("Dec")==0)
			calendar.set(Calendar.MONTH, Calendar.DECEMBER);
		
		calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dayModel.getSelectedItem().toString()));
		calendar.set(Calendar.YEAR, Integer.parseInt(yearModel.getSelectedItem().toString()));
		
		return calendar.getTime();
	}

	private void updateDaySelection() {
		int mon = monthDropDown.getSelectedIndex();
		switch(mon) {
		case 0: 
		case 2:
		case 4:
		case 6:
		case 7:
		case 9:
		case 11: dayModel = new DefaultComboBoxModel(day31);
				break;
		case 3:
		case 5:
		case 8:
		case 10: dayModel = new DefaultComboBoxModel(day30);
			break;
		case 1: 
			int year = Integer.parseInt((yearModel.getSelectedItem().toString()));
			if (year%4==0) {
				dayModel = new DefaultComboBoxModel(day29);
			}
			else {
				dayModel = new DefaultComboBoxModel(day28);
			}
		}
		dayDropDown.setModel(dayModel);
	}
	
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub
		updateDaySelection();
	}
}
